<template>
  <div class="home">
    <section class="data_section">
      <header class="section_title">数据统计</header>
      <el-row :gutter="20"
              style="margin-bottom:10px">
        <el-col :span="4">
          <div class="data_list today_head">
            <span class="data_num head">当日数据:</span>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="data_list">
            <span class="data_num">{{userCount}}</span>新增用户
          </div>
        </el-col>
        <el-col :span="4">
          <div class="data_list">
            <span class="data_num">{{orderCount}}</span>新增订单
          </div>
        </el-col>
        <el-col :span="4">
          <div class="data_list">
            <span class="data_num">{{adminCount}}</span>新增管理员
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="20"
              style="margin-bottom:10px">
        <el-col :span="4">
          <div class="data_list all_head">
            <span class="data_num head">总数据:</span>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="data_list">
            <span class="data_num">{{allUserCount}}</span>注册用户
          </div>
        </el-col>
        <el-col :span="4">
          <div class="data_list">
            <span class="data_num">{{allOrderCount}}</span>订单
          </div>
        </el-col>
        <el-col :span="4">
          <div class="data_list">
            <span class="data_num">{{allAdminCount}}</span>管理员
          </div>
        </el-col>
      </el-row>
    </section>
    <tendency :sevenDate="sevenDate"
              :sevenDay="sevenDay"></tendency>
  </div>
</template>

<script type="text/ecmascript-6">
import dtime from 'time-formater'
import tendency from '../components/tendency'
import { userCount, orderCount, adminCount, allUserCount, allOrderCount, allAdminCount } from '../api/getDate1'
export default {
  name: 'homePage',
  data () {
    return {
      userCount: null,
      orderCount: null,
      adminCount: null,
      allUserCount: null,
      allOrderCount: null,
      allAdminCount: null,
      sevenDate: [[], [], []],
      sevenDay: []
    }
  },
  mounted () {
    this.initData() // 在vue实例创建完成之后 立马加载执行 数据初始化
    for (let i = 6; i > -1; i--) {
      const date = dtime(new Date().getTime() - 86400000 * i).format(
        'YYYY-MM-DD'
      )
      // console.log(date)
      this.sevenDay.push(date)
    }
    // 根据日期获取对应的数据
    this.getSevenDate()
  },
  methods: {
    async initData () {
      console.log(await allUserCount())
      const today = dtime().format('YYYY-MM-DD')
      // 接口 当天新增用户
      const resultUserCount = await userCount(today)
      // 接口 当天新增订单
      const resultOrderCount = await orderCount(today)
      // console.log(resultOrderCount)
      // 接口 当天新增管理员
      const resultAdminCount = await adminCount(today)
      // 接口 所有的用户
      const resultAllUserCount = await allUserCount()
      // 接口 所有的订单
      const resultAllOrderCount = await allOrderCount()
      // 接口 所有的管理员
      const resultAllAdminCount = await allAdminCount()
      Promise.all([
        resultUserCount,
        resultOrderCount,
        resultAdminCount,
        resultAllUserCount,
        resultAllOrderCount,
        resultAllAdminCount
      ]).then(res => {
        // console.log(res);
        this.userCount = res[0].count
        this.orderCount = res[1].count
        this.adminCount = res[2].count
        this.allUserCount = res[3].count
        this.allOrderCount = res[4].count
        this.allAdminCount = res[5].count
      })
    },
    async getSevenDate () {
      const apiArr = [[], [], []]
      this.sevenDay.forEach((item, index) => {
        Promise.all([
          userCount(item),
          orderCount(item),
          adminCount(item)
        ]).then(res => {
          // console.log(res[0], '**************')
          apiArr[0].push(res[0].count)
          apiArr[1].push(res[1].count)
          apiArr[2].push(res[2].count)
        })
      })
      this.sevenDate = apiArr
      // console.log(apiArr)
      const promiseArr = [apiArr[0], apiArr[1], apiArr[2]]
      // console.log(promiseArr)
      Promise.all(promiseArr)
        .then(res => {
          const resArr = [[]]
          res.forEach((item, index) => {
            resArr[Math.floor(index / 7)].push(item)
          })
          // console.log(resArr)
          this.sevenDate = resArr[0]
        })
        .catch(err => {
          console.log(err)
        })
    }
  },
  components: {
    tendency
  }
}
</script>

<style lang="less" scoped>
@import "../style/main.less";
.data_section {
  padding: 20px;
  margin-bottom: 40px;
  .section_title {
    text-align: center;
    font-size: 30px;
    margin-bottom: 10px;
  }
}
.data_list {
  text-align: center;
  font-size: 14px;
  color: #666;
  border-radius: 6px;
  background-color: #e5e9f2;
  .data_num {
    color: #333;
    font-size: 26px;
  }
  .head {
    border-radius: 6px;
    font-size: 22px;
    padding: 4px 0;
    color: #fff;
    display: inline-block;
  }
}
.today_head {
  background: #ff9800;
}
.all_head {
  background: #20a0ff;
}
.wan {
  .sc(16px, #333);
}
</style>
